package com.xiangmo.travel.Utils;

import com.xiangmo.travel.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 转换用户密码
 * 这里使用 bcrypt 加密方式
 */
@Component
public class PasswordUtil {
    // 加密
    public String bcryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String hashedPassword = passwordEncoder.encode(password);
        System.out.println("原密码：" + password);
        System.out.println("加密后的密码：" + hashedPassword);
        return hashedPassword;
    }
    // 校验 password 用户输入的密码， sqlPassword 数据库存储密码
    public boolean checkPassword(String password, String sqlPassword){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.matches(password, sqlPassword);
    }
}
